<template>
	<om-addpage :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit" :showHistoryBtn="false" :saveBtnText="'确认'">
		<om-autoform ref="autoform" :initConfigData="initConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
		import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				initValue:{
					salaryPosId:[],
					overTimeTypeId:[],
					overTimeTypeName:[],
					overTimeTypeRatio:[]
				},
				userIdCache: "deviceStocktakingTemplate-userId-Cache",
				managerIdCache: "deviceStocktakingTemplate-managerId-Cache",
				relatedPartiesIdCache: "deviceStocktakingTemplate-relatedPartiesId-Cache",
				prevPage: "",
				spotCheckMinNum: 0,
				spotCheckMaxNum: 0,
				initConfigData: [{
					key: "title0",
					editors: [{
							checkedit: null,
							data: [],
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							nameField: "inventoryTypeName",
							onSelect: "onSelectInventoryTypeHandle",
							clearFunc: "clearInventoryTypeHandle",
							prompt: "请选择",
							required: true,
							title: "盘点类型",
							type: "selectbox",
							valueField: "inventoryTypeId"
						},{
							checkedit: null,
							data: [],
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							nameField: "assetCategoryName",
							onSelect: "onSelectAssetCategoryHandle",
							prompt: "请选择",
							required: false,
							title: "资产类别",
							type: "selectbox",
							valueField: "assetCategoryId"
						},{
							checkedit: null,
							data: [],
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							nameField: "useSituationName",
							onSelect: "onSelectUseSituationHandle",
							prompt: "请选择",
							required: false,
							title: "使用状态",
							type: "selectbox",
							valueField: "useSituationId"
						},{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							link: "",
							linkParam: {},
							max: "",
							min: "",
							nameField: "userName",
							prompt: "请选择",
							required: false,
							title: "使用人",
							type: "linkbox",
							valueField: "userId",
						},{
							checkedit: null,
							data: [],
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							nameField: "userPositionName",
							onSelect: "onSelectUsePositionHandle",
							prompt: "请选择",
							required: false,
							title: "使用岗位",
							type: "selectbox",
							valueField: "userPositionId"
						},{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							nameField: "userDepartmentName",
							required: false,
							title: "使用部门",
							type: "labelbox",
							valueField: "userDepartmentId"
						},{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							link: "",
							linkParam: {},
							max: "",
							min: "",
							nameField: "managerName",
							prompt: "请选择",
							required: false,
							title: "管理负责人",
							type: "linkbox",
							valueField: "managerId",
						},{
							checkedit: null,
							data: [],
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							nameField: "managementPositionName",
							onSelect: "onSelectManagementPositionHandle",
							prompt: "请选择",
							required: false,
							title: "管理岗位",
							type: "selectbox",
							valueField: "managementPositionId"
						},{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							nameField: "managementDepartmentName",
							required: false,
							title: "管理部门",
							type: "labelbox",
							valueField: "managementDepartmentId"
						},{
							checkedit: null,
							data: [],
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							nameField: "assetLendingName",
							onSelect: "onSelectAssetLendingHandle",
							prompt: "请选择",
							required: false,
							title: "资产借出",
							type: "selectbox",
							valueField: "assetLendingId"
						},{
							checkedit: null,
							data: [],
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							nameField: "stakeholderTypesName",
							onSelect: "onSelectStakeholderTypesHandle",
							prompt: "请选择",
							required: false,
							title: "借用相关单位类型",
							type: "selectbox",
							valueField: "stakeholderTypesId"
						},{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							link: "",
							linkParam: {},
							max: "",
							min: "",
							nameField: "relatedPartiesName",
							prompt: "请选择",
							required: false,
							title: "借用相关单位名称",
							type: "linkbox",
							valueField: "relatedPartiesId",
						},{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							nameField: "spotCheckNum",
							onSelect: "onInputSpotCheckNum",
							required: true,
							title: "本表抽盘数量",
							type: "numberbox",
							valueField: "spotCheckNum"
						}
					]
				}]
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			onSelectInventoryTypeHandle(data){
				let _that = this;
				if(data.code == '40581'){//按使用部门
					_that.$refs.autoform.clear('title0', 'managerId');
					_that.$refs.autoform.clear('title0', 'managementPositionId');
					_that.$refs.autoform.clear('title0', 'managementDepartmentId');
					_that.$refs.autoform.clear('title0', 'assetLendingId');
					_that.$refs.autoform.clear('title0', 'stakeholderTypesId');
					_that.$refs.autoform.clear('title0', 'relatedPartiesId');
					_that.$refs.autoform.changeEditor("title0","managerId",'type',"labelbox")
					_that.$refs.autoform.changeEditor("title0","managementPositionId",'type',"labelbox")
					_that.$refs.autoform.changeEditor("title0","assetLendingId",'type',"labelbox")
					_that.$refs.autoform.changeEditor("title0","stakeholderTypesId",'type',"labelbox")
					_that.$refs.autoform.changeEditor("title0","relatedPartiesId",'type',"labelbox")
					_that.$refs.autoform.changeEditor("title0","userId",'type',"linkbox")
					_that.$refs.autoform.changeEditor("title0","userPositionId",'type',"selectbox")
				}
				if(data.code == '40582'){//按管理部门
					_that.$refs.autoform.clear('title0', 'userId');
					_that.$refs.autoform.clear('title0', 'userPositionId');
					_that.$refs.autoform.clear('title0', 'userDepartmentId');
					_that.$refs.autoform.clear('title0', 'assetLendingId');
					_that.$refs.autoform.clear('title0', 'stakeholderTypesId');
					_that.$refs.autoform.clear('title0', 'relatedPartiesId');
					_that.$refs.autoform.changeEditor("title0","userId",'type',"labelbox")
					_that.$refs.autoform.changeEditor("title0","userPositionId",'type',"labelbox")
					_that.$refs.autoform.changeEditor("title0","assetLendingId",'type',"labelbox")
					_that.$refs.autoform.changeEditor("title0","stakeholderTypesId",'type',"labelbox")
					_that.$refs.autoform.changeEditor("title0","relatedPartiesId",'type',"labelbox")
					_that.$refs.autoform.changeEditor("title0","managerId",'type',"linkbox")
					_that.$refs.autoform.changeEditor("title0","managementPositionId",'type',"selectbox")
				}
				if(data.code == '40583'){//按资产借出
					_that.$refs.autoform.clear('title0', 'userId');
					_that.$refs.autoform.clear('title0', 'userPositionId');
					_that.$refs.autoform.clear('title0', 'userDepartmentId');
					_that.$refs.autoform.clear('title0', 'managerId');
					_that.$refs.autoform.clear('title0', 'managementPositionId');
					_that.$refs.autoform.clear('title0', 'managementDepartmentId');
					_that.$refs.autoform.changeEditor("title0","userId",'type',"labelbox")
					_that.$refs.autoform.changeEditor("title0","userPositionId",'type',"labelbox")
					_that.$refs.autoform.changeEditor("title0","managerId",'type',"labelbox")
					_that.$refs.autoform.changeEditor("title0","managementPositionId",'type',"labelbox")
					_that.$refs.autoform.changeEditor("title0","assetLendingId",'type',"selectbox")
					_that.$refs.autoform.changeEditor("title0","stakeholderTypesId",'type',"selectbox")
					_that.$refs.autoform.changeEditor("title0","relatedPartiesId",'type',"linkbox")
				}
			},
			clearInventoryTypeHandle(){
				let _that = this;
				_that.$refs.autoform.clear('title0', 'inventoryTypeId');
				_that.$refs.autoform.clear('title0', 'userId');
				_that.$refs.autoform.clear('title0', 'userPositionId');
				_that.$refs.autoform.clear('title0', 'userDepartmentId');
				_that.$refs.autoform.clear('title0', 'managerId');
				_that.$refs.autoform.clear('title0', 'managementPositionId');
				_that.$refs.autoform.clear('title0', 'managementDepartmentId');
				_that.$refs.autoform.clear('title0', 'assetLendingId');
				_that.$refs.autoform.clear('title0', 'stakeholderTypesId');
				_that.$refs.autoform.clear('title0', 'relatedPartiesId');
				_that.$refs.autoform.changeEditor("title0","userId",'type',"linkbox")
				_that.$refs.autoform.changeEditor("title0","userPositionId",'type',"selectbox")
				_that.$refs.autoform.changeEditor("title0","managerId",'type',"linkbox")
				_that.$refs.autoform.changeEditor("title0","managementPositionId",'type',"selectbox")
				_that.$refs.autoform.changeEditor("title0","assetLendingId",'type',"selectbox")
				_that.$refs.autoform.changeEditor("title0","stakeholderTypesId",'type',"selectbox")
				_that.$refs.autoform.changeEditor("title0","relatedPartiesId",'type',"linkbox")
			},
			onInputSpotCheckNum(value){
				let _that = this
				if(Number(_that.spotCheckMaxNum)<Number(value)){
					_that.$refs.autoform.formdata.spotCheckNum = _that.spotCheckMaxNum
				}
				if(Number(_that.spotCheckMinNum)>Number(value)){
					_that.$refs.autoform.formdata.spotCheckNum = _that.spotCheckMinNum
				}
			},
			onSelectStakeholderTypesHandle(data){
				let _that = this
				changeEditor("title0","relatedPartiesId",{
					link:'../../public/pageSearch/pageSearch',
					linkParam:{
						other:JSON.stringify({
							stakeholderTypesId: data.id
						}),
						url:'propertyWholeProjectLedgerShow/getStakeholder',
						namefield: 'stakeholderName',
						extrafield:'stakeholderCode',
						searchfield: "q",
						initload:true,
						cachename:_that.relatedPartiesIdCache
					}
				},null,_that.initConfigData)
			},
			formSubmit() {
				let _that = this
				_that.om.request(
					"propertyWholeProjectLedgerShow/getListBySomething",
					{
						assetCategoryId: _that.$refs.autoform.formdata.assetCategoryId,
						useSituationId: _that.$refs.autoform.formdata.useSituationId,
						userId: _that.$refs.autoform.formdata.userId,
						userPositionId: _that.$refs.autoform.formdata.userPositionId,
						managerId: _that.$refs.autoform.formdata.managerId,
						managementPositionId: _that.$refs.autoform.formdata.managementPositionId,
						assetLendingId: _that.$refs.autoform.formdata.assetLendingId,
						stakeholderTypesId: _that.$refs.autoform.formdata.stakeholderTypesId,
						relatedPartiesId: _that.$refs.autoform.formdata.relatedPartiesId,
						spotCheckNum: _that.$refs.autoform.formdata.spotCheckNum,
					}
				).then(res=>{
					if(res.length > 0){
						try {
							for(let i=0; i<res.length; i++){
							    res[i].assetId = res[i].id
							}
						    uni.setStorageSync('deviceStocktakingTemplateData',res);
							uni.navigateBack()
						} catch (e) {
						    // error
						}
					}else{
						_that.om.toast("没有符合条件的数据！")
					}
				})
			},
			initLoad(options) {
				let _that = this
				let pro1 = _that.om.request("dictZero/getListByCode",{code:'40580'})
				let pro2 = _that.om.request("tsysdict/getCombobox",{code:'39143'})
				let pro3 = _that.om.request("tsysdict/getCombobox",{code:'39445'})
				let pro4 = _that.om.request("dictZero/getListByCode",{code:'36924'})
				let pro5 = _that.om.request("tsysdict/getCombobox",{code:'40286'})
				let pro6 = _that.om.request("dataDict/getDataByCode1",{code:'DD023003005'})
				Promise.all([pro1, pro2, pro3, pro4, pro5, pro6]).then(res => {
					changeEditor("title0", "inventoryTypeId", "data", configSelectData(res[0], "id", "dicname"), _that.initConfigData)
					changeEditor("title0", "assetCategoryId", "data", configSelectData(res[1], "id", "dicname"), _that.initConfigData)
					changeEditor("title0", "useSituationId", "data", configSelectData(res[2], "id", "dicname"), _that.initConfigData)
					changeEditor("title0", "assetLendingId", "data", configSelectData(res[3], "id", "dicname"), _that.initConfigData)
					changeEditor("title0", "stakeholderTypesId", "data", configSelectData(res[4], "id", "dicname"), _that.initConfigData)

					_that.spotCheckMinNum = res[5].value.minNum
					_that.spotCheckMaxNum = res[5].value.maxNum
				})
				
				changeEditor("title0","userId",{
					link:'../../public/searchUser/searchUser',
					linkParam:{
						other:JSON.stringify({
						}),
						isSingle:true,
						isUserPos:true,
						cachename:_that.userIdCache
					}
				},null,_that.initConfigData)
				
				changeEditor("title0","managerId",{
					link:'../../public/searchUser/searchUser',
					linkParam:{
						other:JSON.stringify({
						}),
						isSingle:true,
						isUserPos:true,
						cachename:_that.managerIdCache
					}
				},null,_that.initConfigData)
				
				_that.$nextTick(function() {
					
				})
			}
		},
		onLoad(options) {
			let _that = this
			uni.setNavigationBarTitle({
				title: '新增模板'
			});
			Object.assign(_that.initValue, _that.formdata)
			_that.initLoad(options);
		},
		onShow() {
			let _that = this
			let pages = getCurrentPages()
			_that.prevPage = pages[pages.length - 2]
			if (_that.$refs.autoform) {
				uni.getStorage({
					key: _that.userIdCache,
					success: (res) => {
						if(res.data){
							let d = res.data[0]
							let formdata = _that.$refs.autoform.formdata
							formdata.userId = d.id
							formdata.userName = d.username
							formdata.userPositionId = d.posId
							formdata.userPositionName = d.posName
							formdata.userDepartmentId = d.instId
							formdata.userDepartmentName = d.instName
							_that.om.request("orgInstPosUserOfficial/getPosComboboxByUser", {
								userId:d.id
							}).then(res => {
								_that.$refs.autoform.changeEditor("title0","userPositionId","data",configSelectData(res,'id','posname'))
								_that.$refs.autoform.initSelectIndex("title0","userPositionId")
							})
							uni.removeStorage({
								key: _that.userIdCache
							})
						}
					}
				})
				uni.getStorage({
					key: _that.managerIdCache,
					success: (res) => {
						if(res.data){
							let d = res.data[0]
							let formdata = _that.$refs.autoform.formdata
							formdata.managerId = d.id
							formdata.managerName = d.username
							formdata.managementPositionId = d.posId
							formdata.managementPositionName = d.posName
							formdata.managementDepartmentId = d.instId
							formdata.managementDepartmentName = d.instName
							_that.om.request("orgInstPosUserOfficial/getPosComboboxByUser", {
								userId:d.id
							}).then(res => {
								_that.$refs.autoform.changeEditor("title0","managementPositionId","data",configSelectData(res,'id','posname'))
								_that.$refs.autoform.initSelectIndex("title0","managementPositionId")
							})
							uni.removeStorage({
								key: _that.managerIdCache
							})
						}
					}
				})
				uni.getStorage({
					key: _that.relatedPartiesIdCache,
					success: (res) => {
						if(res.data){
							let d = res.data[0]
							let formdata = _that.$refs.autoform.formdata
							formdata.relatedPartiesId = d.stakeholderId
							formdata.relatedPartiesName = d.stakeholderName
							uni.removeStorage({
								key: _that.relatedPartiesIdCache
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</dataStrstyle>
